<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="name" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <!-- <el-input v-model="listQuery.accountId" placeholder="accountId" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" /> -->
      <el-input v-model="listQuery.uid" placeholder="uid" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        Search
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="50">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="公会ID" width="220">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="工会名" width="200">
        <template slot-scope="scope">
          [{{ scope.row.shortName }}]{{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="160">
        <template slot-scope="scope">
          {{ scope.row.createTime | formatDate }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="积分" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.score }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="星星" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.star }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="工会宣言">
        <template slot-scope="scope">
          {{ scope.row.desc }}
        </template>
      </el-table-column>
      <el-table-column label="人数" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.num }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.joinStatus }}
        </template>
      </el-table-column> -->
      <el-table-column label="工会成员名单" width="110" align="center">
        <template slot-scope="scope">
          <router-link :to="'/union/members/'+scope.row.id">
            <el-button type="primary" size="mini">
              工会成员
            </el-button>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link :to="'/union/info/'+scope.row.id">
            <el-button type="primary" size="mini">
              查看
            </el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageNum" @pagination="fetchData" />
  </div>
</template>

<script>
import { formatDate } from '@/utils/date'
import { unionList } from '@/api/union'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on

export default {
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    formatDate(time) {
      var date = new Date(time * 1000)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        page: 0,
        pageNum: 20,
        name: ''
      },
      dialogFormVisible: false
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      unionList(this.listQuery).then(response => {
        this.total = response.data.num
        this.list = response.data.list
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    }
  }
}
</script>
